<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>签到系统</title>
    <style>
        :root {
            --primary-color: #4f46e5;
            --secondary-color: #7c3aed;
            --success-color: #10b981;
            --danger-color: #ef4444;
            --warning-color: #f59e0b;
            --info-color: #3b82f6;
            --light-color: #f8fafc;
            --dark-color: #1e293b;
            --border-radius: 16px;
            --shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 0;
            margin: 0;
            color: var(--dark-color);
        }

        .main-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .content-wrapper {
            display: flex;
            flex: 1;
            gap: 25px;
            margin: 0 20px;
        }

        .sidebar {
            width: 320px;
            flex-shrink: 0;
        }

        .main-content {
            flex: 1;
        }

        @media (max-width: 992px) {
            .content-wrapper {
                flex-direction: column;
            }

            .sidebar {
                width: 100%;
            }
        }

        .app-header {
            text-align: center;
            color: white;
            margin: 20px 0 40px;
            padding: 30px 20px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
        }

        .app-header h1 {
            font-size: 3rem;
            font-weight: 700;
            margin-bottom: 15px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }

        .app-header p {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        .card {
            border: none;
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
            margin-bottom: 25px;
            overflow: hidden;
            transition: var(--transition);
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }

        .card-header {
            background-color: white;
            border-bottom: 1px solid #f0f0f0;
            padding: 15px 20px;
            font-weight: 600;
            color: var(--dark-color);
        }

        .card-body {
            padding: 20px;
        }

        .user-selector {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            gap: 15px;
        }

        .form-control {
            border-radius: var(--border-radius);
            border: 1px solid #e0e0e0;
            padding: 12px 15px;
            transition: var(--transition);
            width: 100%;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.25rem rgba(79, 70, 229, 0.15);
        }

        .form-label {
            font-weight: 500;
            margin-bottom: 8px;
            display: block;
            color: var(--dark-color);
        }

        .user-selector {
            margin-bottom: 15px;
        }

        .btn {
            border-radius: var(--border-radius);
            padding: 12px 20px;
            font-weight: 500;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 1rem;
            border: none;
            cursor: pointer;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            box-shadow: 0 4px 6px -1px rgba(79, 70, 229, 0.3), 0 2px 4px -1px rgba(79, 70, 229, 0.2);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(79, 70, 229, 0.3), 0 4px 6px -2px rgba(79, 70, 229, 0.2);
        }

        .btn-success {
            background: linear-gradient(135deg, var(--success-color) 0%, #059669 100%);
            color: white;
            box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.3), 0 2px 4px -1px rgba(16, 185, 129, 0.2);
        }

        .btn-success:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(16, 185, 129, 0.3), 0 4px 6px -2px rgba(16, 185, 129, 0.2);
        }

        .calendar-container {
            margin-top: 20px;
        }

        .calendar-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .calendar-title {
            font-size: 1.2rem;
            font-weight: 600;
            color: var(--dark-color);
        }

        .calendar {
            display: grid;
            grid-template-columns: repeat(7, 1fr);
            gap: 10px;
            margin-bottom: 20px;
        }

        .calendar-day-header {
            text-align: center;
            font-weight: 600;
            color: var(--dark-color);
            padding: 10px;
        }

        .calendar-day {
            aspect-ratio: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            border-radius: 10px;
            position: relative;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .calendar-day:hover {
            transform: scale(1.05);
        }

        .calendar-day-number {
            font-size: 1.2rem;
            font-weight: 500;
        }

        .calendar-day-signed {
            background-color: var(--success-color);
            color: white;
            box-shadow: 0 4px 8px rgba(16, 185, 129, 0.3);
        }

        .calendar-day-today {
            border: 2px solid var(--warning-color);
            box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.2);
        }

        .calendar-day-future {
            opacity: 0.5;
        }

        .calendar-day-past:not(.calendar-day-signed) {
            background-color: var(--light-color);
        }

        .stats-container {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
            text-align: center;
        }

        .stat-item {
            padding: 15px;
        }

        .stat-value {
            font-size: 2rem;
            font-weight: 700;
            color: var(--primary-color);
        }

        .stat-label {
            font-size: 0.9rem;
            color: #6b7280;
        }

        .message-item {
            background-color: var(--light-color);
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 15px;
            transition: transform 0.3s ease;
        }

        .message-item:hover {
            transform: translateX(5px);
        }

        .message-content {
            margin-bottom: 10px;
            font-size: 1rem;
            color: var(--dark-color);
        }

        .message-time {
            font-size: 0.8rem;
            color: #6b7280;
        }

        .message-user {
            font-weight: 600;
            color: var(--primary-color);
            margin-right: 5px;
        }



        .result-message {
            padding: 15px;
            border-radius: 10px;
            margin-top: 15px;
            animation: fadeIn 0.5s ease;
        }

        .result-message.success {
            background-color: rgba(16, 185, 129, 0.1);
            color: var(--success-color);
            border-left: 4px solid var(--success-color);
        }

        .result-message.error {
            background-color: rgba(239, 68, 68, 0.1);
            color: var(--danger-color);
            border-left: 4px solid var(--danger-color);
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .consecutive-days {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 15px;
            padding: 15px;
            background-color: rgba(99, 102, 241, 0.1);
            border-radius: 10px;
        }

        .consecutive-days i {
            font-size: 1.5rem;
            color: var(--warning-color);
            margin-right: 10px;
        }

        .consecutive-days-text {
            font-size: 1.1rem;
            color: var(--dark-color);
        }

        .consecutive-days-number {
            font-weight: 700;
            font-size: 1.3rem;
            color: var(--warning-color);
            margin: 0 5px;
        }

        /* 替代Bootstrap的样式 */
        .row {
            display: flex;
            flex-wrap: wrap;
            margin-right: -15px;
            margin-left: -15px;
        }

        .col-lg-4 {
            flex: 0 0 33.333333%;
            max-width: 33.333333%;
            padding-right: 15px;
            padding-left: 15px;
        }

        .col-lg-8 {
            flex: 0 0 66.666667%;
            max-width: 66.666667%;
            padding-right: 15px;
            padding-left: 15px;
        }

        .w-100 {
            width: 100% !important;
        }

        .mb-0 {
            margin-bottom: 0 !important;
        }

        .mb-3 {
            margin-bottom: 1rem !important;
        }

        .mt-2 {
            margin-top: 0.5rem !important;
        }

        .py-4 {
            padding-top: 1.5rem !important;
            padding-bottom: 1.5rem !important;
        }

        .text-center {
            text-align: center !important;
        }

        .text-muted {
            color: #6c757d !important;
        }

        .btn-sm {
            padding: 0.25rem 0.5rem;
            font-size: 0.875rem;
        }

        .btn-outline-primary {
            color: var(--primary-color);
            border-color: var(--primary-color);
            background-color: transparent;
        }

        .btn-outline-primary:hover {
            color: white;
            background-color: var(--primary-color);
        }

        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
            padding: 0.75rem 1.25rem;
            margin-bottom: 1rem;
            border: 1px solid transparent;
            border-radius: 0.25rem;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .col-lg-4, .col-lg-8 {
                flex: 0 0 100%;
                max-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="main-container">
        <header class="app-header">
            <h1>📅 每日签到系统</h1>
            <p>坚持每一天，成就更好的自己</p>
        </header>

        <div class="content-wrapper">
            <!-- 侧边栏 -->
            <div class="sidebar">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">✅ 签到</h5>
                    </div>
                    <div class="card-body">
                        <div class="user-selector">
                            <label for="userName" class="form-label">用户名:</label>
                            <input type="text" class="form-control" id="userName" value="用户1" style="max-width: 150px;">
                        </div>
                        <button class="btn btn-primary w-100" onclick="signIn()">
                            ✔️ 立即签到
                        </button>
                        <div id="signInResult"></div>
                        <div class="consecutive-days" id="consecutiveDays" style="display: none;">
                            🔥
                            <span class="consecutive-days-text">已连续签到</span>
                            <span class="consecutive-days-number" id="consecutiveDaysNumber">0</span>
                            <span class="consecutive-days-text">天</span>
                        </div>
                    </div>
                </div>

                <!-- 留言卡片 -->
                <div class="card" style="margin-top: 25px;">
                    <div class="card-header">
                        <h5 class="mb-0">💬 留言板</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <textarea class="form-control" id="messageContent" rows="3" placeholder="记录今天的心情..."></textarea>
                        </div>
                        <button class="btn btn-success w-100" onclick="addMessage()">
                            📤 提交留言
                        </button>
                        <div id="messageResult"></div>
                    </div>
                </div>
            </div>

            <!-- 主内容区 -->
            <div class="main-content">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">📆 本月签到情况</h5>
                    </div>
                    <div class="card-body">
                        <div class="calendar-container">
                            <div class="calendar-header">
                                <div class="calendar-title" id="currentMonth"></div>
                                <button class="btn btn-sm btn-outline-primary" onclick="getSignInStatus()">
                                    🔄 刷新
                                </button>
                            </div>
                            <div class="calendar" id="signInCalendar"></div>
                            <div class="stats-container">
                                <div class="stat-item">
                                    <div class="stat-value" id="signedDays">0</div>
                                    <div class="stat-label">已签到天数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-value" id="totalDays">0</div>
                                    <div class="stat-label">本月总天数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-value" id="signRate">0%</div>
                                    <div class="stat-label">签到率</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 留言列表卡片 -->
                <div class="card" style="margin-top: 25px;">
                    <div class="card-header">
                        <h5 class="mb-0">💬 全部留言</h5>
                    </div>
                    <div class="card-body">
                        <div id="messageList"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // API基础URL配置
        // 如果您在IDE中直接打开HTML文件，请将此URL设置为Spring Boot应用的完整地址
        // 例如：const API_BASE_URL = 'http://localhost:8080';
        // 如果通过Spring Boot应用访问，则使用空字符串
        const API_BASE_URL = '';

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 设置当前月份
            const now = new Date();
            const monthNames = ["一月", "二月", "三月", "四月", "五月", "六月", 
                "七月", "八月", "九月", "十月", "十一月", "十二月"];
            document.getElementById('currentMonth').textContent = 
                `${now.getFullYear()}年 ${monthNames[now.getMonth()]}`;

            // 初始化页面数据
            getSignInStatus();
            getMessages();
        });

        // 签到函数
        function signIn() {
            const userName = document.getElementById('userName').value;

            fetch(`${API_BASE_URL}/api/signin/${userName}`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                const resultDiv = document.getElementById('signInResult');
                const consecutiveDaysDiv = document.getElementById('consecutiveDays');

                if (data.success) {
                    resultDiv.innerHTML = `
                        <div class="result-message success">
                            ✅ ${data.message}
                        </div>
                    `;

                    // 显示连续签到天数
                    document.getElementById('consecutiveDaysNumber').textContent = data.consecutiveDays;
                    consecutiveDaysDiv.style.display = 'flex';

                    // 签到后刷新签到状态
                    console.log('签到成功，刷新签到状态');
                    setTimeout(() => {
                        getSignInStatus();
                    }, 300); // 添加短暂延迟，确保后端数据已更新
                } else {
                    resultDiv.innerHTML = `
                        <div class="result-message error">
                            ❌ ${data.message}
                        </div>
                    `;
                }
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('signInResult').innerHTML = `
                    <div class="result-message error">
                        ❌ 签到失败: ${error.message}
                    </div>
                `;
            });
        }

        // 获取签到状态
        function getSignInStatus() {
            const userName = document.getElementById('userName').value;
            console.log(`获取用户 ${userName} 的签到状态`);

            fetch(`${API_BASE_URL}/api/signin/${userName}/status`)
            .then(response => {
                console.log('API响应状态:', response.status);
                return response.json();
            })
            .then(data => {
                console.log('签到状态数据:', data);
                const calendarDiv = document.getElementById('signInCalendar');
                const signedDaysElement = document.getElementById('signedDays');
                const totalDaysElement = document.getElementById('totalDays');
                const signRateElement = document.getElementById('signRate');

                // 清空日历
                calendarDiv.innerHTML = '';

                // 添加星期标题
                const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
                weekDays.forEach(day => {
                    const dayDiv = document.createElement('div');
                    dayDiv.className = 'calendar-day-header';
                    dayDiv.textContent = day;
                    calendarDiv.appendChild(dayDiv);
                });

                // 获取当前日期
                const now = new Date();
                const today = now.getDate();
                const currentMonth = now.getMonth();
                const currentYear = now.getFullYear();

                // 添加每日签到状态
                const signInStatus = data.signInStatus;

                for (let i = 0; i < signInStatus.length; i++) {
                    const dayDiv = document.createElement('div');
                    dayDiv.className = 'calendar-day';

                    // 创建日期数字元素
                    const dayNumber = document.createElement('div');
                    dayNumber.className = 'calendar-day-number';
                    dayNumber.textContent = i + 1;
                    dayDiv.appendChild(dayNumber);

                    // 添加签到图标
                    if (signInStatus[i]) {
                        const icon = document.createElement('div');
                        icon.innerHTML = '✓';
                        dayDiv.appendChild(icon);
                        dayDiv.classList.add('calendar-day-signed');
                    } else if (i + 1 < today) {
                        // 过去未签到的日期
                        dayDiv.classList.add('calendar-day-past');
                    } else if (i + 1 > today) {
                        // 未来日期
                        dayDiv.classList.add('calendar-day-future');
                    }

                    // 标记今天
                    if (i + 1 === today) {
                        dayDiv.classList.add('calendar-day-today');
                    }

                    calendarDiv.appendChild(dayDiv);
                }

                // 更新统计信息
                const signedCount = data.signedCount;
                const daysInMonth = data.daysInMonth;
                const signRate = Math.round((signedCount / daysInMonth) * 100);

                signedDaysElement.textContent = signedCount;
                totalDaysElement.textContent = daysInMonth;
                signRateElement.textContent = `${signRate}%`;
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('signInCalendar').innerHTML = `
                    <div class="alert alert-danger">
                        ⚠️ 获取签到状态失败: ${error.message}
                    </div>
                `;
            });
        }

        // 添加留言
        function addMessage() {
            const userName = document.getElementById('userName').value;
            const content = document.getElementById('messageContent').value;

            if (!content.trim()) {
                document.getElementById('messageResult').innerHTML = `
                    <div class="result-message error">
                        ❌ 请输入留言内容
                    </div>
                `;
                return;
            }

            fetch(`${API_BASE_URL}/api/signin/${userName}/message`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ content })
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('messageResult').innerHTML = `
                    <div class="result-message success">
                        ✅ 留言成功
                    </div>
                `;
                document.getElementById('messageContent').value = '';

                // 添加留言后刷新留言列表
                getMessages();
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('messageResult').innerHTML = `
                    <div class="result-message error">
                        ❌ 留言失败: ${error.message}
                    </div>
                `;
            });
        }

        // 获取留言列表
        function getMessages() {
            const userName = document.getElementById('userName').value;

            fetch(`${API_BASE_URL}/api/signin/${userName}/messages`)
            .then(response => response.json())
            .then(messages => {
                const messageListDiv = document.getElementById('messageList');
                messageListDiv.innerHTML = '';

                if (messages.length === 0) {
                    messageListDiv.innerHTML = `
                        <div class="text-center text-muted py-4">
                            💬 <span style="font-size: 2rem;"></span>
                            <p class="mt-2">暂无留言</p>
                        </div>
                    `;
                    return;
                }

                messages.forEach(message => {
                    const messageDiv = document.createElement('div');
                    messageDiv.className = 'message-item';

                    const createTime = new Date(message.createTime).toLocaleString();
                    messageDiv.innerHTML = `
                        <div class="message-content">
                            <span class="message-user">${message.userName}:</span> ${message.content}
                        </div>
                        <div class="message-time">
                            🕐 ${createTime}
                        </div>
                    `;

                    messageListDiv.appendChild(messageDiv);
                });
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('messageList').innerHTML = `
                    <div class="alert alert-danger">
                        ⚠️ 获取留言失败: ${error.message}
                    </div>
                `;
            });
        }
    </script>
</body>
</html>